package com.jane.fragment.homepagefragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.jane.adapter.homepageadapter.MeAttentionAdapter;
import com.jane.bean.HomePageGridViewBean;
import com.jane.bean.JsonData;
import com.jane.config.NetConfig;
import com.jane.gzzb.R;
import com.jane.utils.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeAttentionFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    //申明
    private static final String TAG = MeAttentionFragment.class.getSimpleName();
    private View mView;
    private SwipeRefreshLayout mRefresh;
    private ListView mListView;
    private List<HomePageGridViewBean> mYes;
    private List<HomePageGridViewBean> mNot;
    private List<List<HomePageGridViewBean>> lists;
    private String path;
    private MeAttentionAdapter mAdapter;

    public MeAttentionFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_me_attention, null);
        initView();
        initData();
        initListener();
        return mView;
    }

    private void initView() {
        mRefresh = (SwipeRefreshLayout) mView.findViewById(R.id.fragment_me_attention_refresh);
        mRefresh.setColorSchemeColors(Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN);//样式颜色
        mListView = (ListView) mView.findViewById(R.id.fragment_me_attention_listview);
    }

    private void initData() {
        lists = new ArrayList<>();
        mYes = new ArrayList<>();
        mNot = new ArrayList<>();
        mAdapter = new MeAttentionAdapter(getActivity(), lists);
        mListView.setAdapter(mAdapter);

        path = NetConfig.WODEGUANZHU_URL;
        String jsonString = Utils.getJsonString(path);
        if (!TextUtils.isEmpty(jsonString)) {
            parseData(jsonString);
            ;//解析数据
        } else {
            //判断是否有网
            if (!Utils.isNetwork(getActivity())) {
                return;
            }
            //网络加载数据
            netLoadData();
        }
    }

    private void initListener() {
        mRefresh.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        lists.clear();//先清空数据
        netLoadData();
    }

    private void netLoadData() {
//        Log.e(TAG, "netLoadData: " + path);
        x.http().get(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //保存一份
                JsonData jsonData = new JsonData();
                jsonData.setPath(path);
                jsonData.setJsonString(result);
                Utils.saveJsonString(jsonData);
//                Log.e(TAG, "onSuccess: " + result);
                mRefresh.setRefreshing(false);
                if (!TextUtils.isEmpty(result)) {
                    parseData(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mRefresh.setRefreshing(false);
                Log.e(TAG, "onError: ---------------");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void parseData(String result) {
        try {
            //直播中的数据
            JSONObject obj0 = new JSONObject(result);
            JSONObject obj1 = obj0.getJSONObject("live");
            JSONArray array0 = obj1.getJSONArray("data_list");
            for (int i = 0; i < array0.length(); i++) {
                JSONObject obj2 = array0.getJSONObject(i);
                HomePageGridViewBean bean = new HomePageGridViewBean();
                bean.setRawcoverimage(obj2.optString("rawcoverimage"));
                bean.setRawcommentatorimage(obj2.optString("rawcommentatorimage"));
                bean.setName(obj2.optString("name"));
                bean.setTitle(obj2.optString("title"));
                bean.setSourcename(obj2.optString("sourcename"));
                bean.setCommentator(obj2.optString("commentator"));
                bean.setViewers(obj2.optInt("viewers"));

                mYes.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        lists.add(mYes);
        //未开播的数据
        try {
            //直播中的数据
            JSONObject obj0 = new JSONObject(result);
            JSONObject obj1 = obj0.getJSONObject("no_live");
            JSONArray array0 = obj1.getJSONArray("data_list");
            for (int i = 0; i < array0.length(); i++) {
                JSONObject obj2 = array0.getJSONObject(i);
                HomePageGridViewBean bean = new HomePageGridViewBean();
                bean.setCommentator(obj2.optString("commentator"));
                bean.setRawcommentatorimage(obj2.optString("rawcommentatorimage"));
                bean.setName(obj2.optString("name"));
                bean.setViewers(obj2.optInt("viewers"));
                mNot.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        lists.add(mNot);
        Log.e(TAG, "parseData:mYes.size()  mNot.size()   " + mYes.size() + "," + mNot.size());
        mAdapter.notifyDataSetChanged();
    }
}
